package com.glink.manage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.glink.common.exception.GeneralSimpleException;
import com.glink.common.util.BaseCommonUtils;
import com.glink.common.util.LogUtil;
import com.glink.common.vo.ResponseResult;
import com.glink.manage.dto.constructionwork.*;
import com.glink.manage.service.ConstructionWorkService;
import com.glink.manage.service.ConstructionWorkSiteService;
import com.glink.manage.service.ConstructionWorkUnitRelaService;
import com.glink.manage.vo.constructionwork.*;
import com.glink.manage.vo.participatingunit.ParticipatingUnitListVO;
import com.glink.manage.vo.participatingunit.ParticipatingUnitSimpleDetailInfoVO;
import com.glink.manage.vo.participatingunit.ParticipatingUnitSimpleInfoVO;
import com.glink.manage.vo.person.PersonSimplePhoneInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author  qiushaoshan
 * @since  2025-01-08
 */
@Slf4j
@RestController
@Api(tags = "工程管理")
@RequestMapping("/API/Manage/constructionWorkSite")
public class ConstructionWorkController extends AbstractBaseController {

    @Autowired
    private ConstructionWorkService constructionWorkService;
    
    @Autowired
    private ConstructionWorkUnitRelaService constructionWorkUnitRelaService;
    
    @Autowired
    private ConstructionWorkSiteService constructionWorkSiteService;
    
    @ApiOperation(value = "01.新增施工工程")
    @PostMapping(value = "/insert", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> insertMeasurePoint(@Validated @RequestBody ConstructionWorkSaveDTO dto) throws GeneralSimpleException {
        log.info("01.新增施工工程：{}", LogUtil.encode(dto));
        boolean status = constructionWorkService.saveConstructionWork(dto);
        log.info("01.新增施工工程：success");
        return this.response(status);
    }

    @ApiOperation(value = "02.更新施工工程")
    @PostMapping(value = "/update", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> updateMeasurePoint(@Validated @RequestBody ConstructionWorkUpdateDTO dto) throws GeneralSimpleException {
        log.info("02.更新施工工程：{}", LogUtil.encode(dto));
        boolean status = constructionWorkService.updateConstructionWork(dto);
        log.info("02.更新施工工程：success");
        return this.response(status);
    }

    @ApiOperation(value = "03.根据ID查询施工工程信息")
    @PostMapping(value = "/findById", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<ConstructionWorkBaseVO> findById(@Validated @RequestBody ConstructionWorkIdDTO dto) throws GeneralSimpleException {
        log.info("02.更新施工工程：{}", LogUtil.encode(dto));
        ConstructionWorkBaseVO workServiceById = constructionWorkService.findById(dto);
        log.info("02.更新施工工程：success");
        return this.response(workServiceById);
    }
    
    @ApiOperation(value = "04.分页查询施工工程")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "orgId", value = "当前登录用户的orgId", dataType = "string", required = true),
            @ApiImplicitParam(paramType = "header", name = "userId", value = "当前登录用户的userId", dataType = "string", required = true)})
    @PostMapping(value = "/findByPage", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<IPage<ConstructionWorkPageVO>> findByPage(@Validated @RequestBody ConstructionWorkPageDTO dto) throws GeneralSimpleException {
        log.info("04.分页查询施工工程：{}", LogUtil.encode(dto));
        IPage<ConstructionWorkPageVO> pageVOIPage = constructionWorkService.findByPage(dto);
        log.info("04.分页查询施工工程：success");
        return this.response(pageVOIPage);
    }

    @ApiOperation(value = "05.导出施工工程")
    @PostMapping(value = "/exportExcel", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void exportExcel(@Validated @RequestBody ConstructionWorkExportDTO dto, HttpServletResponse response) throws Exception {
        log.info("05.导出施工工程{}", LogUtil.encode(dto));
        constructionWorkService.exportExcel(dto, response);
    }

    @ApiOperation(value = "06.分页查询参建单位")
    @PostMapping(value = "/findUnitByPage", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<IPage<ParticipatingUnitSimpleInfoVO>> findUnitByPage(@Validated @RequestBody ConstructionWorkUnitPageDTO dto) throws Exception {
        log.info("06.分页查询参建单位{}", LogUtil.encode(dto));
        IPage<ParticipatingUnitSimpleInfoVO> simpleInfoVOIPage = constructionWorkService.findUnitByPage(dto);
        log.info("06.分页查询参建单位：success");
        return this.response(simpleInfoVOIPage);
    }

    @ApiOperation(value = "07.保存参建单位")
    @PostMapping(value = "/saveUnit", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> saveUnit(@Validated @RequestBody ConstructionWorkUnitSaveDTO dto) throws Exception {
        log.info("07.保存参建单位{}", LogUtil.encode(dto));
        Boolean status = constructionWorkUnitRelaService.saveUnit(dto);
        log.info("07.保存参建单位：success");
        return this.response(status);
    }

    @ApiOperation(value = "07-01.修改工程与参建单位关联信息")
    @PostMapping(value = "/updateUnitRelaInfo", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> updateUnitRelaInfo(@Validated @RequestBody ConstructionWorkUnitSaveDTO dto) throws Exception {
        log.info("07-01.修改工程与参建单位关联信息{}", LogUtil.encode(dto));
        Boolean status = constructionWorkUnitRelaService.updateUnitRelaInfo(dto);
        log.info("07-01.修改工程与参建单位关联信息：success");
        return this.response(status);
    }

    @ApiOperation(value = "08.移除参建单位")
    @PostMapping(value = "/delUnit", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> delUnit(@Validated @RequestBody ConstructionWorkUnitIdDTO dto) throws Exception {
        log.info("08.移除参建单位{}", LogUtil.encode(dto));
        Boolean status = constructionWorkUnitRelaService.delUnit(dto);
        log.info("08.移除参建单位：success");
        return this.response(status);
    }

    @ApiOperation(value = "08-01.移除参建单位-查询准入还未失效的用户信息")
    @PostMapping(value = "/findDelUnitExistValidAccess", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<List<String>> findDelUnitExistValidAccess(@Validated @RequestBody ConstructionWorkUnitIdDTO dto) throws Exception {
        log.info("08-01.移除参建单位-校验是否可以移除{}", LogUtil.encode(dto));
        List<String> accessNameList = constructionWorkUnitRelaService.findDelUnitExistValidAccess(dto);
        log.info("08-01.移除参建单位-校验是否可以移除：success");
        return this.response(accessNameList);
    }

    @ApiOperation(value = "09.分页查询工程现场")
    @PostMapping(value = "/findSiteByPage", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<IPage<ConstructionWorkSitePageVO>> findSiteByPage(@Validated @RequestBody ConstructionWorkSitePageDTO dto) throws Exception {
        log.info("09.分页查询工程现场{}", LogUtil.encode(dto));
        IPage<ConstructionWorkSitePageVO> simpleInfoVOIPage = constructionWorkSiteService.findSiteByPage(dto);
        log.info("09.分页查询工程现场：success");
        return this.response(simpleInfoVOIPage);
    }

    @ApiOperation(value = "10.新增工程现场")
    @PostMapping(value = "/saveSite", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<String> saveSite(@Validated @RequestBody ConstructionWorkSiteSaveDTO dto) throws Exception {
        log.info("10.新增工程现场{}", LogUtil.encode(dto));
        String siteObjId = constructionWorkSiteService.saveSite(dto);
        log.info("10.新增工程现场：success");
        return this.response(siteObjId);
    }

    @ApiOperation(value = "10-2.修改工程现场")
    @PostMapping(value = "/updateSite", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> updateSite(@Validated @RequestBody ConstructionWorkSiteUpdateDTO dto) throws Exception {
        log.info("10-2.修改工程现场{}", LogUtil.encode(dto));
        Boolean status = constructionWorkSiteService.updateSite(dto);
        log.info("10-2.修改工程现场：success");
        return this.response(status);
    }

    @ApiOperation(value = "10-3.查询工程现场")
    @PostMapping(value = "/findSite", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<ConstructionWorkSiteInfoVO> findSite(@Validated @RequestBody ConstructionWorkSiteIdDTO dto) throws Exception {
        log.info("10-3.查询工程现场 {}", LogUtil.encode(dto));
        ConstructionWorkSiteInfoVO siteInfoVO = constructionWorkSiteService.findSite(dto);
        log.info("10-3.查询工程现场：success");
        return this.response(siteInfoVO);
    }

    @ApiOperation(value = "11.移除工程现场")
    @PostMapping(value = "/delSite", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> delSite(@Validated @RequestBody ConstructionWorkSiteIdDTO dto) throws Exception {
        log.info("11.移除工程现场{}", LogUtil.encode(dto));
        Boolean status = constructionWorkSiteService.delSite(dto);
        log.info("11.移除工程现场：success");
        return this.response(status);
    }

    @ApiOperation(value = "12.根据用户信息查询参建单位列表")
    @GetMapping(value = "/findUnitByWork", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<List<ParticipatingUnitListVO>> findUnitByWork() throws Exception {
        log.info("12.根据用户信息查询参建单位列表");
        String orgId = BaseCommonUtils.extractOrdIdByToken();
        List<ParticipatingUnitListVO> unitListVOList = constructionWorkSiteService.findUnitByWorkAndOrgId(orgId);
        log.info("12.根据用户信息查询参建单位列表：success");
        return this.response(unitListVOList);
    }
    @ApiOperation(value = "13.根据用户所属单位查询工程列表")
    @GetMapping(value = "/findWorkList", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<List<ConstructionWorkListVO>> findWorkList() throws Exception {
        log.info("13.根据用户所属单位查询工程列表");
        List<ConstructionWorkListVO> workListVOList = constructionWorkService.findWorkListByOrgId();
        log.info("13.根据用户所属单位查询工程列表：success");
        return this.response(workListVOList);
    }

    @ApiOperation(value = "14.根据工程id查询施工现场列表")
    @PostMapping(value = "/findWorkSiteListByWorkId/{workObjId}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<List<ConstructionWorkSiteListVO>> findWorkSiteListByWorkId(@PathVariable("workObjId")String workObjId)  {
        log.info("14.根据工程id查询施工现场列表");
        List<ConstructionWorkSiteListVO> constructionWorkSiteListVOList = constructionWorkSiteService.findWorkSiteListByWorkId(workObjId);
        log.info("14.根据工程id查询施工现场列表：success");
        return this.response(constructionWorkSiteListVOList);
    }
    
    @ApiOperation(value = "15.根据地名模糊搜索地名信息")
    @PostMapping(value = "/findPlaceName", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<List<ConstructionWorkSitePlaceNameVO>> findPlaceName(@Validated @RequestBody ConstructionWorkSitePlaceNameDTO dto) throws Exception {
        log.info("14.根据工程id查询施工现场列表");
        List<ConstructionWorkSitePlaceNameVO> placeNameVOList = constructionWorkSiteService.findPlaceName(dto);
        log.info("14.根据工程id查询施工现场列表：success");
        return this.response(placeNameVOList);
    }

    @ApiOperation(value = "16.获取厂站机器码")
    @PostMapping(value = "/findStationMachineCode", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<ConstructionWorkSiteGrantVO> findStationMachineCode(@RequestBody @Validated ConstructionWorkSiteIdDTO dto) throws GeneralSimpleException {
        log.info("16.获取厂站机器码：{}", LogUtil.encode(dto));
        return this.response(constructionWorkSiteService.findSiteMachineCode(dto));
    }

    @ApiOperation(value = "17.施工现场授权")
    @PostMapping(value = "/grantStation", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> grantStation(@RequestBody @Validated ConstructionWorkSiteGrantDTO dto) throws GeneralSimpleException {
        log.info("17.施工现场授权：{}", LogUtil.encode(dto));
        return this.response(constructionWorkSiteService.grantSite(dto));
    }

    @ApiOperation(value = "18.根据参建单位ID获取参建单位下的人员信息列表")
    @PostMapping(value = "/findPersonManageInfoByUnitId", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<List<PersonSimplePhoneInfoVO>> findPersonManageInfoByUnitId(@RequestBody @Validated ConstructionWorkUnitIdDTO dto) throws GeneralSimpleException {
        log.info("18.根据参建单位ID获取参建单位下的人员信息列表：{}", LogUtil.encode(dto));
        return this.response(constructionWorkSiteService.findPersonManageInfoByUnitId(dto));
    }

    @ApiOperation(value = "19.根据施工工程-参建单位关联ID获取详细关联信息")
    @PostMapping(value = "/findWorkUnitRealDetailInfoByUnitRealId", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<ParticipatingUnitSimpleDetailInfoVO> findWorkUnitRealDetailInfoByUnitRealId(@RequestBody @Validated ConstructionWorkUnitRealIdDTO dto)  {
        log.info("19.根据施工工程-参建单位关联ID获取详细关联信息：{}", LogUtil.encode(dto));
        return this.response(constructionWorkSiteService.findWorkUnitRealDetailInfoByUnitRealId(dto));
    }

}
